BUILD_DIR = ./build
ENTRY_POINT = 0xc0001500
tool_dir= /usr1/newpc/tool/bochs-2.4.6
target_dir = /usr1/newpc/build/
src_dir = $(shell pwd)
AS = nasm
CC = gcc
LD = ld
LIB = -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/
ASFLAGS = -f elf
ASFLAGS2 = -I ${src_dir}/boot/include/
CFLAGS = -m32 -Wall $(LIB) -c -fno-builtin -fno-stack-protector -W -Wmissing-prototypes #-Wstrict-prototypes
LDFLAGS = -m elf_i386 -Ttext $(ENTRY_POINT) -e main -Map $(BUILD_DIR)/kernel.map
OBJS = $(BUILD_DIR)/main.o $(BUILD_DIR)/init.o $(BUILD_DIR)/interrupt.o $(BUILD_DIR)/timer.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/print.o $(BUILD_DIR)/debug.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/string.o $(BUILD_DIR)/bitmap.o

############ C 代码编译 ##############
$(BUILD_DIR)/main.o: kernel/main.c \
	lib/kernel/print.h lib/stdint.h \
	kernel/init.h kernel/memory.h
	$(CC) $(CFLAGS) $< -o $@
	
$(BUILD_DIR)/init.o: kernel/init.c kernel/init.h \
       	lib/kernel/print.h lib/stdint.h \
	kernel/interrupt.h \
	device/timer.h \
	kernel/memory.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/interrupt.o: kernel/interrupt.c kernel/interrupt.h \
        lib/stdint.h kernel/global.h lib/kernel/io.h lib/kernel/print.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/timer.o: device/timer.c device/timer.h \
	lib/stdint.h lib/kernel/io.h lib/kernel/print.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/debug.o: kernel/debug.c kernel/debug.h \
	lib/kernel/print.h lib/stdint.h kernel/interrupt.h
	$(CC) $(CFLAGS) $< -o $@


$(BUILD_DIR)/string.o: lib/string.c lib/string.h \
	kernel/global.h lib/stdint.h\
	kernel/debug.h \
        lib/kernel/print.h kernel/interrupt.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/bitmap.o: lib/kernel/bitmap.c lib/kernel/bitmap.h \
        lib/string.h kernel/debug.h \
	lib/kernel/print.h lib/stdint.h kernel/interrupt.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/memory.o: kernel/memory.c kernel/memory.h \
        lib/kernel/bitmap.h \
	lib/kernel/print.h lib/stdint.h kernel/interrupt.h
	$(CC) $(CFLAGS) $< -o $@

############ ASM 代码编译 ##############
$(BUILD_DIR)/kernel.o: kernel/kernel.S
	$(AS) $(ASFLAGS) $< -o $@

$(BUILD_DIR)/print.o: lib/kernel/print.S
	$(AS) $(ASFLAGS) $< -o $@

$(BUILD_DIR)/mbr: boot/mbr.S
	$(AS) $(ASFLAGS2) $< -o $@

$(BUILD_DIR)/loader: boot/loader.S
	$(AS) $(ASFLAGS2) $< -o $@

############     链接     ##############
$(BUILD_DIR)/kernel.bin: $(OBJS)
	$(LD) $(LDFLAGS) $^ -o $@
	strip --remove-section=.note.gnu.property $(BUILD_DIR)/kernel.bin

.PHONY: mk_dir hd clean build_loader make_biximage copy_image all

mkdir:
	if[[ ! -d $(BUILD_DIR) ]];then mkdir $(BUILD_DIR);fi

make_biximage:
	${tool_dir}/bximage -hd -mode="flat" -size=60 -q $(BUILD_DIR)/hd60M.img

build: $(BUILD_DIR)/kernel.bin

build_loader: $(BUILD_DIR)/mbr $(BUILD_DIR)/loader

hd:
	dd if=${BUILD_DIR}/mbr of=$(BUILD_DIR)/hd60M.img bs=512 count=1 conv=notrunc
	echo "write mbr to hd60M.img"
	dd if=${BUILD_DIR}/loader of=$(BUILD_DIR)/hd60M.img bs=512 count=4 seek=2 conv=notrunc
	echo "write loader to hd60M.img"
	dd if=$(BUILD_DIR)/kernel.bin of=$(BUILD_DIR)/hd60M.img bs=512 count=200 seek=9 conv=notrunc
	echo "write kernel.bin to hd60M.img"

copy_image:
	cp $(BUILD_DIR)/hd60M.img ${target_dir}/

clean:
	cd $(BUILD_DIR) && rm -f ./*

all: mk_dir make_biximage build build_loader  hd copy_image


